package com.ruoyi.tcp.entity.beat;

import com.ruoyi.tcp.entity.CustomerBaseEntity;
import com.ruoyi.tcp.util.TcpAesUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * @author 蒋翔 SY100242
 * @date 2024/8/7 15:17
 * @email SY100242@hnlens.com
 */
@Setter
@Getter
@ToString
public class ServerMessageEntity extends CustomerBaseEntity {

    private static final long serialVersionUID = 1L;

    //设备编号
    private Long deviceNumber;
    //响应消息长度
    private Long msgLen;
    //响应消息
    private String msg;


    @Override
    public byte[] getOtherBytes() {
        ByteBuf buffer = Unpooled.buffer();
        byte[] deviceNumberBytes = TcpAesUtil.longToByteArray16(deviceNumber);
        buffer.writeBytes(deviceNumberBytes);
        long len=0;
        if(StringUtils.hasLength(msg)){
            String asciiStr = TcpAesUtil.asciiToHex(msg);
            len = Objects.requireNonNull(TcpAesUtil.hex2byte(asciiStr)).length;
        }
        byte[] bytes = TcpAesUtil.longToByteArray8(len);
        buffer.writeBytes(bytes);

        if(StringUtils.hasLength(msg)){
            String asciiStr = TcpAesUtil.asciiToHex(msg);
            buffer.writeBytes(TcpAesUtil.hex2byte(asciiStr));
        }
        return ByteBufUtil.getBytes(buffer);
    }
}
